$(document).ready(function () {
    function getData(curr) {

        $.ajax({
            type: "get",
            url: "http://www.puer.zone:30000/UserManageInfo",
            async: true,
            data: {
                page: curr,
                row: 12
            },
            success: function (data) {
                dataView(data[0]);
                pageV(data[1]);
            }
        });

    }

    getData(1);

    function dataView(data) {
        for (var i = 0; i < data.length; i++) {
            var delText;
            if (data[i][2] !== '高级管理员') {
                delText = '<a class="edit_num">删除账号</a>';
            } else {
                delText = '无权限'
            }
            var tabsText = `<tr>
      					<td>${data[i][0]}</td>
      					<td>${data[i][1]}</td>
      					<td>${data[i][2]}</td>
      					<td>${data[i][3]}</td>
      					<td>${data[i][4]}</td>
      					<td index="${data[i][0]}">${delText}</td>
      		 		</tr>`;
            $('.data_cont').append(tabsText);
        }

        $('.edit_num').click(function () {

            $.ajax({
                type: "get",
                url: "http://www.puer.zone:30000/DelManage",
                async: true,
                data: {
                    UserId: $(this).parent().attr('index')
                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('删除成功', {time: 500}, function () {
                            location.reload();
                        })
                    }
                }
            });
        })

    }

    //添加管理员
    $('.new_add').click(function () {
        var Index;
        console.log($.cookie('UserRight'))
        var Text = $('.select2-selection__rendered').text();
        switch (Text) {
            case '高级管理员':
                Index = 1
                break;
            case '普通管理员':
                Index = 2
                break;
            case '商品管理员':
                Index = 3
                break;
            case '订单管理员':
                Index = 4
                break;

        }

        if ($('.cl_inp3').val() == '') {
            layer.msg('登录账号不能为空')

        } else if ($('.cl_inp2').val() == '') {
            layer.msg('登录名称不能为空')

        } else if ($('.cl_inp4').val() == '') {
            layer.msg('登录密码不能为空')
        } else {

            if ($.cookie('UserRight') == 1 || $.cookie('UserRight') == '10086') {
                $.ajax({
                    type: "get",
                    url: "http://www.puer.zone:30000/NewBuildManage",
                    async: true,
                    data: {
                        UserNo: $('.cl_inp3').val(),
                        UserName: $('.cl_inp2').val(),
                        Password: $('.cl_inp4').val(),
                        Role: Index,
                        UserId: $.cookie('UserId')
                    },
                    success: function (data) {
                        if (data[0] == 'failure') {
                            if (data[1] == 'No') {
                                console.log(1)
                                layer.msg('登录账号重复')
                            } else if (data[1] == 'Name') {
                                layer.msg('登录名重复')
                            } else if (data[1] == 'error') {
                                layer.msg('新增异常请重试')
                            }
                        } else {
                            layer.msg('新增成功', {time: 500}, function () {
                                location.reload();
                            })
                        }
                    }
                });
            } else {
                layer.msg('无权限');
            }

        }
    })


    function pageV(count) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'demo0'
                , limit: 12
                , count: count
                , jump: function (obj, first) {
                    // console.log(obj.curr)
                    if (!first) {
                        $('.data_cont').html('');
                        getData(obj.curr)
                    }
                }
            });
        })
    }
})
